package com.zhouyipeng.wirte;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 写文件控制文件大小
 *
 * @author zhouyipeng
 * @since 2022-01-28
 */
public class FileLength extends File {
    static long maxLength = 1024 * 1024 * 10;//文件中最大字节，默认长度
    File file = null;


    public static void main(String[] args) {

        List<String> mobileList = Arrays.asList("18556505992","18556505992","18556505992","18556505992");

        mobileList.forEach(mobile -> {
            byte[] bytes = (mobile + "/n").getBytes();
            File file = new File("/Users/zhouyipeng/Desktop/2022-01-28/"+ System.currentTimeMillis());
            write(file,bytes);
        });

        String str = "18556505992";
        byte[] byteArr = str.getBytes();
        // print the byte[] elements
        System.out.println("String to byte array: " + Arrays.toString(byteArr));

    }


    public FileLength(String fileName) {
        super(fileName);
        file = new File(fileName);
    }


    public long getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(long maxLength) {
        this.maxLength = maxLength;
    }

    public static void write(File file, byte[] b) {
        File f = file;
        FileOutputStream fos = null;
        if (file.length() + b.length > maxLength) {//判断是否超过最大长度，写入下一个文件
            String filePath = getNewFilePath(file);
            write(new File(filePath), b);
        } else {
            try {
                fos = new FileOutputStream(f, true);
                fos.write(b);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     *
     */
    public FileLength(File parent, String child) {
        super(parent, child);


    }

    /**
     * 判断字符串是否是数字
     */
    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 获得文件数索引
     */
    public static String getFileIndex(File f) {
        String filePath = f.getPath();
        int endIndex = filePath.lastIndexOf(".");
        String fileName = filePath.substring(0, endIndex);
        String str = "";
        for (int i = 0; i < fileName.length(); i++) {
            String a = fileName.charAt(i) + "";
            if (isInteger(a)) {
                str += a;
            }
        }
        return str;
    }

    /**
     * 将文件名称中的数字移除-获取文件名称
     */
    public static String getFileName(File f) {
        String filePath = f.getPath();
        int beginIndex = filePath.lastIndexOf("\\/");
        int endIndex = filePath.lastIndexOf(".");
        String fileName = filePath.substring(beginIndex + 1, endIndex);
        String fileNewName = "";
        for (int i = 0; i < fileName.length(); i++) {
            String a = fileName.charAt(i) + "";
            if (isInteger(a)) {
                fileNewName = fileName.substring(0, i);
                break;
            }
        }
        return fileNewName;
    }

    /**
     * 获取新文件的路径
     */
    public static String getNewFilePath(File f) {
        String filePath = f.getPath();
        int index = filePath.lastIndexOf(".");
        String newFilePath = "";
        if (FileLength.isInteger(filePath.substring(index - 1, index))) {
            newFilePath = getFileName(f) + (Integer.parseInt(getFileIndex(f)) + 1) + ".txt";
        } else {
            newFilePath = filePath.substring(0, index - 1) + "1.txt";
        }
        return newFilePath;
    }
}
